﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Vds2465Telegrams
{
	[TestClass]
	public class UserData_70_DataBlock_Test
	{
		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test01_Equals()
		{
			byte[] bytes = new byte[] { 0x01, 0x02, 0xff, 0xbb };
			UserData_70_DataBlock userData = new UserData_70_DataBlock(0x99, bytes);

			{
				// completely different type - NOT equal
				object dummyObject = new object();
				Assert.IsFalse(userData.Equals(dummyObject));
			}

			{
				// different derived type 
				UserData_11_Error userData2Compare = new UserData_11_Error(eErrorCode.DataOutOfRange, 0x99);
				Assert.IsFalse(userData.Equals(userData2Compare));
			}

			{
				// same object - equal
				UserData_70_DataBlock userData2Compare = new UserData_70_DataBlock(userData.Device, userData.GetCopyOfBytes());
				Assert.IsTrue(userData.Equals(userData2Compare));
			}

			{
				// different property values - NOT equal                
				UserData_70_DataBlock userData2Compare = new UserData_70_DataBlock((byte)(userData.Device + 1), userData.GetCopyOfBytes());
				Assert.IsFalse(userData.Equals(userData2Compare));
			}

			{
				// different property values - NOT equal
				byte[] bytesOriginal = userData.GetCopyOfBytes();
				byte[] bytes2Compare = new byte[bytesOriginal.Length];
				for (int index = 0; index < bytesOriginal.Length; index++)
				{
					bytes2Compare[index] = bytesOriginal[index];
				}
				// change the last byte
				if (bytesOriginal.Length > 0)
				{
					bytes2Compare[bytesOriginal.Length - 1] = (byte) (bytesOriginal[bytesOriginal.Length - 1] + 0x44);
				}
				UserData_70_DataBlock userData2Compare = new UserData_70_DataBlock(userData.Device, bytes2Compare);
				Assert.IsFalse(userData.Equals(userData2Compare));
			}
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test02_Equals_BytesNull()
		{
			byte[] bytes = null;
			UserData_70_DataBlock userData = new UserData_70_DataBlock(0x99, bytes);

			{
				// completely different type - NOT equal
				object dummyObject = new object();
				Assert.IsFalse(userData.Equals(dummyObject));
			}

			{
				// different derived type 
				UserData_11_Error userData2Compare = new UserData_11_Error(eErrorCode.DataOutOfRange, 0x99);
				Assert.IsFalse(userData.Equals(userData2Compare));
			}

			{
				// same object - equal
				UserData_70_DataBlock userData2Compare = new UserData_70_DataBlock(userData.Device, userData.GetCopyOfBytes());
				Assert.IsTrue(userData.Equals(userData2Compare));
			}

			{
				// same object - equal
				UserData_70_DataBlock userData2Compare = new UserData_70_DataBlock(userData.Device, new byte[0]);
				Assert.IsTrue(userData.Equals(userData2Compare));
			}

			{
				// different property values - NOT equal                
				UserData_70_DataBlock userData2Compare = new UserData_70_DataBlock((byte)(userData.Device + 1), userData.GetCopyOfBytes());
				Assert.IsFalse(userData.Equals(userData2Compare));
			}

			{
				// different property values - NOT equal
				byte[] bytes2Compare = new byte[] { 0x01, 0x02, 0xff, 0xbb };
				UserData_70_DataBlock userData2Compare = new UserData_70_DataBlock(userData.Device, bytes2Compare);
				Assert.IsFalse(userData.Equals(userData2Compare));
			}
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test03_GetHashCode()
		{
			{
				byte[] bytes = new byte[] { 0x01, 0x02, 0xff, 0xbb };
				UserData_70_DataBlock userData = new UserData_70_DataBlock(0xef, bytes);
				int hashCode = userData.GetHashCode();
			}

			{
				byte[] bytes = null;
				UserData_70_DataBlock userData = new UserData_70_DataBlock(0xef, bytes);
				int hashCode = userData.GetHashCode();
			}

			{
				byte[] bytes = new byte[0];
				UserData_70_DataBlock userData = new UserData_70_DataBlock(0xef, bytes);
				int hashCode = userData.GetHashCode();
			}
		}

		[TestMethod]
		[Owner("Frank Eppink")]
		public void Test04_ToString()
		{
			{
				byte[] bytes = new byte[] { 0x01, 0x02, 0xff, 0xbb };
				UserData_70_DataBlock userData = new UserData_70_DataBlock(0xef, bytes);
				string stringResult = userData.ToString();
				Assert.IsNotNull(stringResult);
			}

			{
				byte[] bytes = null;
				UserData_70_DataBlock userData = new UserData_70_DataBlock(0xef, bytes);
				string stringResult = userData.ToString();
				Assert.IsNotNull(stringResult);
			}

			{
				byte[] bytes = new byte[0];
				UserData_70_DataBlock userData = new UserData_70_DataBlock(0xef, bytes);
				string stringResult = userData.ToString();
				Assert.IsNotNull(stringResult);
			}
		}
	}
}
